<?php
declare (strict_types = 1);

namespace app\controller;

use think\exception\ValidateException;
use think\facade\View;
use think\Request;
use app\model\user as UserModel;
use app\validate\User as UserValidate;
use app\middleware\Auth as AuthMiddleware;
class User
{
    protected $middleware = [AuthMiddleware::class];

    private $toast = 'public/toast';

    /**
     * 显示资源列表
     *
     * @return string
     */
    public function index()
    {

        return view('index',[
            'list' => UserModel::withSearch(['gender','username','email','create_time'],[
                'gender' => request()->param('gender'),
                'username' => request()->param('username'),
                'email' => request()->param('email'),
                'create_time' => request()->param('create_time')
            ])->paginate([
                'list_rows'=> 5,
                'query' => request()->param()
            ]),'orderTime'         =>  request()->param('create_time') == 'desc' ? 'asc' : 'desc'

        ]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        return view('create');

    }

//    public function borrow()
//    {
//        return view('D:\php\phpEnv\www\www.tp.com\view\user\borrow.html');
//    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $data = $request->param();
        //数据验证
        try {
            validate(UserValidate::class)->batch(true)->check($data);
        } catch (ValidateException $exception) {
            return view('view/public/toast.html',[
                'infos'     =>  $exception->getError(),
                'url_text'  =>  '返回上一页',
                'url_path'  =>  url('/user/create')

            ]);
        }
        //密码加密
        $data['password'] = sha1($data['password']);
        //新增后返回id
        $id = UserModel::create($data)->getData('id');

        //关联保存，喜好
        UserModel::find($id)->hobby()->save([
            'content' => $data['hobby']
        ]);
        //提示
        return $id ? view('./view/public/toast.html', [
            'infos' => ['恭喜，添加成功！'],
            'url_text' => '去首页',
            'url_path' => url('/')
        ]) : '添加失败！';
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        return view('edit',[
            'obj' => UserModel::find($id)
        ]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return string
     */
    public function update(Request $request, $id)
    {
        $data = $request->param();

        try {
            validate(UserValidate::class)
                ->scene('edit')
                ->batch(true)
                ->check($data);
        } catch (ValidateException $exception) {
            return view($this->toast, [
                'infos'     =>  $exception->getError(),
                'url_text'  =>  '返回上一页',
                'url_path'  =>  url('/user/'.$id.'/edit')
            ]);
        }

//        //如有密码，则写入
//        if (!empty($data['newpasswordnot'])) {
//            $data['password'] = sha1($data['newpasswordnot']);
//        }

        return UserModel::update($data) ? view('D:\php\phpEnv\www\www.tp.com\view\public\toast.html', [
            'infos'     =>  ['恭喜，修改成功~'],
            'url_text'  =>  '去首页',
            'url_path'  =>  url('/')
        ]) : '修改失败';

    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return string
     */
    public function delete($id)
    {
//        return UserModel::destroy($id) ? view('D:\php\phpEnv\www\www.tp.com\view\public\toast.html', [
        return UserModel::destroy($id) ? view($this->toast, [
            'infos'     =>  ['恭喜，删除成功~'],
            'url_text'  =>  '去首页',
            'url_path'  =>  url('/')
        ]) : '删除失败！';

    }

    /**
     * 上传文传
     */
    public function upload(){
        // 获取表单上传文件
        $files = request()->file('image');
        $savename = [];
        foreach($files as $file){
            $savename[] = \think\facade\Filesystem::putFile( 'topic', $file);
        }

    }

}
